import os
from PIL import Image

dataset_dir = 'data/training/1st_manual'
label_dir = 'data/training/label'  # 替换为你的标签目录路径

# 如果标签目录不存在，则创建它
if not os.path.exists(label_dir):
    os.makedirs(label_dir)

# 遍历1st_manual目录
for gif_file in os.listdir(dataset_dir):
    if gif_file.endswith('.gif'):
        # 构建完整的文件路径
        file_path = os.path.join(dataset_dir, gif_file)
        # 打开GIF文件
        gif_image = Image.open(file_path)

        # 转换为二值图像
        binary_image = gif_image.convert('1')
        # 构建新的文件名和路径
        new_file_path = os.path.join(label_dir, os.path.splitext(gif_file)[0] + '.png')
        # 保存二值PNG图像
        binary_image.save(new_file_path)
        print(f'Converted {gif_file} to {os.path.basename(new_file_path)}')

print("All GIF files have been converted to binary PNG and saved in the label directory.")